package com.zgz.underwrite.action;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.log4j.Logger;

import com.zgz.underwrite.dao.CoverageDao;
import com.zgz.underwrite.dao.DepartmentDao;
import com.zgz.underwrite.dao.EmployeeDao;
import com.zgz.underwrite.dao.InsuranceAppDao;
import com.zgz.underwrite.dao.ProductDao;
import com.zgz.underwrite.dao.SpecialConventionDao;
import com.zgz.underwrite.model.Coverage;
import com.zgz.underwrite.model.Department;
import com.zgz.underwrite.model.Employee;
import com.zgz.underwrite.model.InsuranceApplication;
import com.zgz.underwrite.model.InsuredCvrg;
import com.zgz.underwrite.model.Product;
import com.zgz.underwrite.model.SpecialConvention;
import com.zgz.underwrite.service.PremiumService;
import com.zgz.underwrite.service.UnderwriteService;
import com.zgz.underwrite.util.StringUtil;

public class InsuranceAppAction extends BaseAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = 998270513027480455L;
	
	public final static Logger log = Logger.getLogger(InsuranceAppAction.class);
	
	private InsuranceAppDao insuranceAppDao;
	private InsuranceApplication insuranceApp;
	private DepartmentDao deptDao;
	private ProductDao prodDao;
	private EmployeeDao empDao;
	private CoverageDao cvrgDao;
	private SpecialConventionDao specDao;
	private UnderwriteService underwriteService;

	private String[] cvrgNoCheckbox = new String[]{};
	private String[] specNoCheckbox = new String[]{};
	
	
	/**
	 * 投保向导，初始化数据
	 * @return
	 */
	public String initData(){
		List<Department> depts;
		List<Product> prods;
		try {
			depts = this.deptDao.getAll();
			prods = this.prodDao.getAll();
			super.request.setAttribute("depts", depts);
			super.request.setAttribute("prods", prods);
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ERROR;
	}
	
	/**
	 * 投保下一步操作
	 * @return
	 */
	public String nextOpera(){
		Department dept;
		Product prod;
		Set<Employee> emps;
		Set<Coverage> cvrgs;
		List<SpecialConvention> specs;
		try {
			dept = this.deptDao.get(this.insuranceApp.getDepartment().getSDeptNo());
			prod= this.prodDao.get(this.insuranceApp.getProduct().getSProductCde());
			specs = this.specDao.getAll();
			emps = dept.getEmployees();
			cvrgs = prod.getCoverages();
			super.request.setAttribute("dept", dept);
			super.request.setAttribute("prod", prod);
			super.request.setAttribute("emps", emps);
			super.request.setAttribute("cvrgs", cvrgs);
			super.request.setAttribute("specs", specs);
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ERROR;
	}
	
	/**
	 * 新增投保单
	 * @return
	 */
	public String newInsuranceApp(){
		log.info(">>>>>>>>>>>>>>>>>>>>接收到新增投保单请求......");
		try {
			insuranceApp.setSAppNo(StringUtil.getAppNo());//设置投保单号
			InsuredCvrg insuredCvrg = new InsuredCvrg();
			Set<InsuredCvrg> cvrgSet = new HashSet<InsuredCvrg>();
			Coverage cvrg = null;
			for(String cvrgNo : cvrgNoCheckbox){
				cvrg = new Coverage();
				cvrg.setSCvrgNo(cvrgNo);
				insuredCvrg.setCvrg(cvrg);
				insuredCvrg.setInsuranceApp(insuranceApp);
				cvrgSet.add(insuredCvrg);
			}
			Set<SpecialConvention> specSet = new HashSet<SpecialConvention>();
			SpecialConvention spec = null;
			for(String specNo : specNoCheckbox){
				spec =  new SpecialConvention();
				spec.setSSpecCvtNo(specNo);
				specSet.add(spec);
			}
			this.insuranceApp.setInsuredCvrg(cvrgSet);
			this.insuranceApp.setSpecConventions(specSet);
			this.insuranceAppDao.save(insuranceApp.getApplicant());//先持久化投保人
			this.insuranceAppDao.save(insuranceApp);
			return SUCCESS;
		} catch (Exception e) {
			log.info(">>>>>>>>>>>>>>>>>>>>新增投保单失败......");
			log.info(">>>>>>>>>>>>>>>>>>>>"+e.getMessage());
		}
		return ERROR;
	}
	
	
	/**
	 * 获取需要核保的投保单
	 */
	public String getNeedUnderwriteApp(){
		log.info(">>>>>>>>>>>>>>>>>>>>申请核保.....");
		List<InsuranceApplication> list = null;
		try {
			String hql = "From InsuranceApplication as app where app.status=:status";
			list = this.insuranceAppDao.findAppByParam(hql,"status", "未申请核保");
			super.request.setAttribute("apps", list);
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ERROR;	
	}
	
	/**
	 * 获取自核不通过的列表(退回单列表)
	 * @return
	 */
	public String listBackApp(){
		log.info(">>>>>>>>>>>>>>>>>>>>获取退回单列表.....");
		List<InsuranceApplication> list = null;
		String hql = "From InsuranceApplication as app where app.status=:status ";
		list = this.insuranceAppDao.findAppByParam(hql, "status", "自核不通过");
		super.request.setAttribute("apps", list);
		return SUCCESS;
	}
	
	/**
	 * 查看退回单核保结果
	 * @return
	 */
	public String readUnderwriteResult(){
		try {
			InsuranceApplication app = this.insuranceAppDao.get(this.insuranceApp.getSAppNo());
			super.request.setAttribute("app", app);
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ERROR;
	}
	
	
	/**
	 * 更新退回单
	 * @return
	 */
	public String updateBackApp(){
		//TODO
		return SUCCESS;
	}
	
	/**
	 * 投保单列表
	 * @return
	 */
	public String listAppBySearch(){
		List<InsuranceApplication> list = null;
		try {
			list = this.insuranceAppDao.getAll();
			super.request.setAttribute("apps", list);
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ERROR;
	}
	
	/**
	 * 申请核保
	 * @return
	 */
	public String updateAppAndApplyUnderwrite(){
		this.updateAppStatus();
		try {
			this.insuranceApp = this.insuranceAppDao.get(this.insuranceApp.getSAppNo());
			this.underwriteService.calPremium(this.insuranceApp); //计算保费
			this.underwriteService.autoUnderwrite(this.insuranceApp);//自动核保
			this.insuranceAppDao.update(this.insuranceApp);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}

	
	public void updateAppStatus(){
		try {
			this.insuranceApp = this.insuranceAppDao.get(this.insuranceApp.getSAppNo());
			this.insuranceApp.setStatus("已申请核保");
			this.insuranceAppDao.update(this.insuranceApp);
		} catch (Exception e1) {
			e1.printStackTrace();
		}
	}

	
	public void setInsuranceAppDao(InsuranceAppDao insuranceAppDao) {
		this.insuranceAppDao = insuranceAppDao;
	}

	public InsuranceAppDao getInsuranceAppDao() {
		return insuranceAppDao;
	}

	public void setInsuranceApp(InsuranceApplication insuranceApp) {
		this.insuranceApp = insuranceApp;
	}

	public InsuranceApplication getInsuranceApp() {
		return insuranceApp;
	}




	public void setDeptDao(DepartmentDao deptDao) {
		this.deptDao = deptDao;
	}

	public DepartmentDao getDeptDao() {
		return deptDao;
	}

	public void setProdDao(ProductDao prodDao) {
		this.prodDao = prodDao;
	}

	public ProductDao getProdDao() {
		return prodDao;
	}

	public void setEmpDao(EmployeeDao empDao) {
		this.empDao = empDao;
	}

	public EmployeeDao getEmpDao() {
		return empDao;
	}
	public CoverageDao getCvrgDao() {
		return cvrgDao;
	}



	public void setCvrgDao(CoverageDao cvrgDao) {
		this.cvrgDao = cvrgDao;
	}



	public SpecialConventionDao getSpecDao() {
		return specDao;
	}



	public void setSpecDao(SpecialConventionDao specDao) {
		this.specDao = specDao;
	}




	public void setSpecNoCheckbox(String[] specNoCheckbox) {
		this.specNoCheckbox = specNoCheckbox;
	}



	public String[] getSpecNoCheckbox() {
		return specNoCheckbox;
	}



	public void setCvrgNoCheckbox(String[] cvrgNoCheckbox) {
		this.cvrgNoCheckbox = cvrgNoCheckbox;
	}



	public String[] getCvrgNoCheckbox() {
		return cvrgNoCheckbox;
	}

	public void setUnderwriteService(UnderwriteService underwriteService) {
		this.underwriteService = underwriteService;
	}

	public UnderwriteService getUnderwriteService() {
		return underwriteService;
	}

}
